
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import Entity.DANXUAN_Entity;
import regex_all.Regex_str_pattern;

public class FromList {

    /**
     * 根据ti_pattern2，匹配line是否符合
     * @param line 需要匹配的字符串
     * @param ti_pattern2 匹配的正则表达式 模式
     * @return 返回是否匹配
     */
    public static boolean is_matcher_finder(String line, Pattern ti_pattern2) {
        Matcher matcher=ti_pattern2.matcher(line);
        return matcher.find();
    }

    public static String reg="[A-Z]\\s?\\.[^A-Z\r\n]*";
    public static Pattern xuanxiang_pattern=get_Pattern(reg);

    public static String biaoti="^[一二三四五六七]、?...";//单选，多选
    public static Pattern biaoti_pattern=get_Pattern(biaoti);

    public static String ti_reg="^\\d+\\.\\x20?";// 1. 2. 3. 
    public static Pattern ti_pattern=get_Pattern(ti_reg);

    public static String null_reg="null";//单选，多选
    public static Pattern null_pattern=get_Pattern(null_reg);

    public static String kuohao="[(（]+ +[)）]+";
    public static Pattern kuohao_pattern=get_Pattern(kuohao);

    
    public static List<Pattern> patterns=new ArrayList<>(){{
        // 注意正则表达式匹配顺序
        add(biaoti_pattern);
        add(ti_pattern);
        add(xuanxiang_pattern);
        add(Regex_str_pattern.daan_with_content_pattern);
        add(Regex_str_pattern.jiexi_with_content_pattern);
    }};

    // public static HashMap<Choose_Type,SheetF<DANXUAN_Entity>> sheet_HashMap=new HashMap<>(){{
    //     put(Choose_Type.DANXUAN,new SheetF<DANXUAN_Entity>("单选"));
    //     put(Choose_Type.DUOXUAN, new SheetF<DANXUAN_Entity>("多选"));
    //     put(Choose_Type.PANDUAN, new SheetF<DANXUAN_Entity>("判断"));
    //     put(Choose_Type.WENDA, new SheetF<DANXUAN_Entity>("问答"));
    // }};



    /**
     * 用来转换word文档到excel文档
     * @param book 工作簿文件
     * @param one 一个docx和工作簿文件
     */
    public static void docx_to_excel(BookF book,OneF one) {
        
        Choose_Type type1=Choose_Type.Other;//一级类型，单选、多选、判断
        Ti one_ti=new Ti();
        BookF bookF=book;
        
        List<String> content=new ArrayList<>();
        try {
            content = ReplaceAll_Str.get_list_string_from_docx(one.docx_file,one);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        Stream<String> a=ReplaceAll_Str.all_split_by_hashMap(content);
        a=ReplaceAll_Str.all_replace_by_hashMap(a);
        a=a.map(line->line.trim());
        a=a.filter(line->!line.isEmpty());

        Choose_Type start_type=Choose_Type.Other;
        Choose_Type end_type=Choose_Type.Other;

        try {
            List<String> collect = a.collect(Collectors.toList());

            collect.add("1000000.END");

            int[] idx={0};

            while(idx[0]<collect.size()){

                String line=collect.get(idx[0]);

                if (idx[0]==0 && line != null && line.startsWith("\uFEFF")) { // 检查并去除BOM
                    line = line.substring(1);
                }

                start_type=get_line_type(line);

                // if(start_type==Choose_Type.ti || start_type==Choose_Type.PANDUAN || start_type==Choose_Type.DUOXUAN || start_type==Choose_Type.DANXUAN){
                if( (start_type==Choose_Type.ti || start_type==Choose_Type.DANXUAN || start_type==Choose_Type.DUOXUAN || start_type==Choose_Type.PANDUAN) && type1!=Choose_Type.Other){

                    // 开始提交 data数据
                    DANXUAN_Entity data = one_ti.convert_to_data();
                    if(data!=null){
                        fill_value(data, 0, 1, bookF.book_name);
                        add_data_to_sheet(type1,data,bookF);
                    }
                    one_ti=new Ti();
                }
                String[] strs;
                List<String> get_list;
                switch (start_type) {
                    case DANXUAN:
                        type1=Choose_Type.DANXUAN;
                        break;
                    case DUOXUAN:
                        type1=Choose_Type.DUOXUAN;
                        break;
                    case PANDUAN:
                        type1=Choose_Type.PANDUAN;
                        break;
                    case ti:
                        strs=line.split(ti_reg);
                        String it=strs[1];
                        int sub_idx=first_matcher_idx(it,xuanxiang_pattern);
                        one_ti.timu=it.substring(0,sub_idx);

                        if(sub_idx==it.length()){
                            ;
                        }else{
                            get_list = get_matcher_list(it, xuanxiang_pattern);
                            //  (get_list,xuanxiang_pattern);
                            // 去除类似A.xxx的内容

                            one_ti.xuanxiang.addAll(get_matcher_list_content(get_list, Regex_str_pattern.xuan_dot_pattern));
                        }
                        end_type=Choose_Type.xuanxiang;

                        // 继续处理具有正确，错误标识的题
                        if(is_matcher_finder(it, Regex_str_pattern.zheng_pattern)){
                            one_ti.daan="正确";
                            strs=it.split(Regex_str_pattern.zheng_pattern.toString());
                            one_ti.timu=strs[0]+"( )";
                        }

                        if(is_matcher_finder(it, Regex_str_pattern.cuo_pattern)){
                            one_ti.daan="错误";
                            strs=it.split(Regex_str_pattern.cuo_pattern.toString());
                            one_ti.timu=strs[0]+"( )";
                        }
                        break;
                    case xuanxiang:
                        List<String> get_matcher_list1 = get_matcher_list(line, xuanxiang_pattern);
                        one_ti.xuanxiang.addAll(get_matcher_list_content(get_matcher_list1, Regex_str_pattern.xuan_dot_pattern));
                        end_type=Choose_Type.xuanxiang;
                        break;
                    case daan:
                        List<String> get_matcher_list3 = get_matcher_list(line,Regex_str_pattern.daan_with_content_pattern);
                        strs=get_matcher_list3.get(0).split(Regex_str_pattern.daan);
                        one_ti.daan=strs[1];
                        end_type=Choose_Type.daan;
                        break;
                    case jiexi:
                        get_list= get_matcher_list(line,Regex_str_pattern.jiexi_with_content_pattern);
                        strs=get_list.get(0).split(Regex_str_pattern.jiexi);
                        one_ti.jiexi=strs[1];
                        end_type=Choose_Type.jiexi;
                        break;
                        
                    default:
                        end_type=Choose_Type.Other;
                        // type1=Choose_Type.Other;
                        break;
                }
                idx[0]++;
            }
        } catch (Exception e) {
            one.isYouxiao=false;
            e.printStackTrace();
        }
        // bookF.add_sheet(one);
        // return bookF;
    }

    /**
     * 添加对应数据到sheet
     * @param type1
     * @param data
     */
    private static void add_data_to_sheet(Choose_Type type1, DANXUAN_Entity data,BookF bookF) {
        if(bookF.sheet_HashMap.containsKey(type1)){
            bookF.sheet_HashMap.get(type1).getData().add(data);
        }
    }

    // 寻找it第一次满足匹配的索引，如果不存在，返回字符串长度
    private static int first_matcher_idx(String it, Pattern xuanxiang_pattern2) {
        Matcher matcher=xuanxiang_pattern.matcher(it);
        // 需要先find一下
        boolean f=matcher.find();
        int sub_idx=it.length();
        try {
            if(f){
                sub_idx=matcher.start();
            }
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
        return sub_idx;
    }

    private static Choose_Type get_line_type(String line) {
        // 注意这里，获取对应的line的type类型的优先级
        for(Pattern it:patterns){
            if(is_matcher_finder(line, it)){
                List<String> matcher_strs=get_matcher_list(line,it);

                if(it.equals(biaoti_pattern) &&( matcher_strs.get(0).contains("单选") || matcher_strs.get(0).contains("选择"))){
                    return Choose_Type.DANXUAN;
                }

                if(it.equals(biaoti_pattern) && matcher_strs.get(0).contains("多选")){
                    return Choose_Type.DUOXUAN;
                }

                if(it.equals(biaoti_pattern) && matcher_strs.get(0).contains("判断")){
                    return Choose_Type.PANDUAN;
                }
                
                if(it.equals(ti_pattern)){
                    return Choose_Type.ti;
                }

                if(it.equals(xuanxiang_pattern)){
                    return Choose_Type.xuanxiang;
                }

                if(it.equals(Regex_str_pattern.daan_with_content_pattern)){
                    return Choose_Type.daan;
                }

                if(it.equals(Regex_str_pattern.jiexi_with_content_pattern)){
                    return Choose_Type.jiexi;
                }
            }
        }
        return Choose_Type.Other;
    }

    private static Pattern get_line_pattern(String line) {
        for(Pattern it:patterns){
            if(is_matcher_finder(line, it)){
                return it;
            }
        }
        return null_pattern;
    }

    /**
     * 获取满足正则表达式的所有文本内容
     * @param line
     * @param xuanxiang_pattern2
     * @return 返回所有文本结果，以list作为结果值
     */
    private static List<String> get_matcher_list(String line, Pattern xuanxiang_pattern2) {
        Matcher matcher=xuanxiang_pattern2.matcher(line);
        
        List<String> result=new ArrayList<>();
        while(matcher.find()){
            result.add(matcher.group());
        }
        return result;
    }

    private static List<String> get_matcher_list_content(List<String> list, Pattern xuanxiang_pattern2) {
        List<String> content=new ArrayList<>();
        for (String string : list) {
            // Matcher matcher=xuanxiang_pattern2.matcher(string);
            // List<String> result=new ArrayList<>();
            // while(matcher.find()){
            //     result.add(matcher.group());
            // }
            content.add(string.split(xuanxiang_pattern2.toString())[1]);
        }

        return content;
    }

    private static Pattern get_Pattern(String reg2) {
        return Pattern.compile(reg2);
    }

    private static void fill_value(DANXUAN_Entity data, int i, int entity_idx, String value) {
        switch (entity_idx) {
            case 1:
                data.setStr1(value);
                break;
            case 2:
                data.setStr2(value);
                break;
                case 3:
                data.setStr3(value);
                break;
           
           case 4:
                data.setStr4(value);
                break;
           
           case 5:
                data.setStr5(value);
                break;
           
           case 6:
                data.setStr6(value);
                break;
           
           case 7:
                data.setStr7(value);
                break;
           
           case 8:
                data.setStr8(value);
                break;
           
           case 9:
                data.setStr9(value);
                break;
           
           case 10:
                data.setStr10(value);
                break;
            default:
                break;
        }
    }

}
